﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace GraphList
{
    public class Graph : IGraph
    {
        private IDictionary<string, Vertex> vertices;

        public Graph()
        {
            vertices = new Dictionary<string, Vertex>();
        }

        //PRE: An url and a vertex is created.
        //POST: A vertex is added to a dictionary with url as key and the vertex as value.
        public void AddVertex(Vertex vertex)
        {
            vertices.Add(vertex.Url, vertex);
        }

        public bool HasEdge(Vertex v1, Vertex v2)
        {
            throw new NotImplementedException();
        }

        public IList<Vertex> GetAdjacenciesTo(Vertex vertex)
        {
            List<Vertex> adjacenciesTo = new List<Vertex>();
            foreach (Edge edge in vertex.Edges)
            {
                adjacenciesTo.Add(edge.To);
            }

            return adjacenciesTo;
        }

        public void Unmark()
        {
            throw new NotImplementedException();
        }

        public void Bfs(Vertex v)
        {
            throw new NotImplementedException();
        }

        public void Dfs(Vertex v)
        {
            throw new NotImplementedException();
        }

        // Returns a dictionary with the vertices in the graph.
        public IDictionary<string, Vertex> Vertices
        {
            get { return vertices; }
        }

        //public void AddEdgeWithWeight(Vertex from, Vertex to, int weight)
        //{
        //    Edge edgeObj = new Edge(from, to, weight);

        //    ((LinkedList<Edge>)vertices[from]).AddLast(edgeObj);
        //}

        public void AddEdge(Vertex from, Vertex to)
        {
            from.AddEdge(to);
        }
        
        //Prints the graph in the console.
        public void PrintGraph()
        {
            int count = 0;
            foreach (KeyValuePair<string, Vertex> keyValue in Vertices)
            {
                Console.WriteLine();
                Console.WriteLine("Navnet på vertex: " + keyValue.Value.Url);
                Console.WriteLine("   Edges:");
                foreach (Edge edgeObj in keyValue.Value.Edges)
                {
                    Console.WriteLine("    " + edgeObj.To.Url);
                }
                count++;
            }
            Console.WriteLine();
            Console.WriteLine("Number of Vertexes: " + count);
        }

        // Checks whether that the url(and therefor the vertex) in the parameter exist or not exist as key in the dictionary.
        public bool ContainsVertex(string url)
        {
            bool found = false;
            IEnumerator<KeyValuePair<string, Vertex>> i = vertices.GetEnumerator();
            while (!found && i.MoveNext())
            {
                string currentUrl = i.Current.Key;
                if (currentUrl.Equals(url))
                {
                    found = true;
                }
            }
            return found;
        }
    }
}
